package com.zrrd.yt.weball.controller;

import com.zrrd.yt.common.result.Result;
import com.zrrd.yt.list.client.ListFeignClient;
import com.zrrd.yt.model.list.SearchParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

/**
 * @author HongyueYang
 */
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    //搜索商品
    @GetMapping("/list.html")
    public String search(SearchParam param, Model model){
        //调用feign请求搜索服务
        Result<Map> result = listFeignClient.search(param);

        //将数据存入到model中
        model.addAllAttributes(result.getData());

        //存入记录拼接的url
        String urlParam = this.makeUrlParam(param);
        model.addAttribute("urlParam",urlParam);

        //存入记录的排序Map
        Map<String, Object> map = this.makeOrderMap(param.getOrder());
        model.addAttribute("orderMap",map);

        //存入品牌回显数据
        String trademark = this.makeTrademark(param.getTrademark());
        model.addAttribute("trademarkParam",trademark);

        //存入平台属性回显数据
        List<Map<String, String>> list = this.makeProps(param.getProps());
        model.addAttribute("propsParamList",list);

        //转发给页面
        return "list/index";
    }

    //根据前端传入的搜索条件制作出参数的url字符串
    private String makeUrlParam(SearchParam param){
        StringBuilder builder = new StringBuilder();
        //判断关键字
        if (param.getKeyword()!=null){
            builder.append("keyword=").append(param.getKeyword());
        }
        //判断分类
        if (param.getCategory1Id()!=null){
            builder.append("category1Id=").append(param.getCategory1Id());
        }
        if (param.getCategory2Id()!=null){
            builder.append("category2Id=").append(param.getCategory2Id());
        }
        if (param.getCategory3Id()!=null){
            builder.append("category3Id=").append(param.getCategory3Id());
        }
        //判断品牌
        if (param.getTrademark()!=null){
            if (builder.length()>0){
                builder.append("&trademark=").append(param.getTrademark());
            }
        }
        //判断平台属性
        if (param.getProps()!=null){
            for (String prop : param.getProps()) {
                if (builder.length()>0){
                    builder.append("&props=").append(prop);
                }
            }
        }
        //返回结果
        return "list.html?"+builder.toString();
    }

    //根据前端传入的排序字符串制作出对应的排序规则Map
    private Map<String,Object> makeOrderMap(String order){
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }

    //制作品牌回显数据
    private String makeTrademark(String trademark){
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            if (split!=null && split.length==2){
               return "品牌："+split[1];
            }
        }
        return null;
    }

    //制作平台属性回显数据
    private List<Map<String,String>> makeProps(String[] props){
        List<Map<String,String>> list = new ArrayList<>();
        if (props!=null && props.length>0){
            for (String prop : props) {
                String[] split = prop.split(":");
                Map<String,String> map = new HashMap<>();
                map.put("attrId",split[0]);
                map.put("attrValue",split[1]);
                map.put("attrName",split[2]);
                list.add(map);
            }
        }
        return list;
    }
}
